﻿using Draco.OWF.Metadata.DataBinding;
using System;
using System.Collections.Generic;
using System.Xml.Serialization;

namespace Draco.OWF.Metadata
{
    public class ActivityModel
    {
        protected String m_Id = Guid.NewGuid().ToString();
        protected List<BindingDataSchema> m_ExceptionSchemas = new List<BindingDataSchema>();

        private ActivityRender m_Render;

        public ActivityModel()
        {
            this.Name = "No_Name";
            this.Configuration = new ActivityConfiguration();
        }

        public ActivityModel(String activityType)
        {
            this.Name = "No_Name";
            this.ActivityType = activityType;
            this.Configuration = new ActivityConfiguration();
        }

        [XmlAttribute("id")]
        public virtual string Id
        {
            get { return m_Id; }
            set
            {
                if (!String.IsNullOrEmpty(value))
                    m_Id = value;
            }
        }

        [XmlAttribute("name")]
        public virtual String Name { get; set; }

        [XmlAttribute("type")]
        public virtual String ActivityType { get; set; }

        [XmlAttribute("entry")]
        public virtual String EntryProperty { get; set; }
        [XmlIgnore]
        public virtual Boolean Entry { get {return "true"==EntryProperty; } set { EntryProperty = value ? "true" : "false"; } }

        [XmlArray("Configuration", Order = 1)]
        [XmlArrayItem("item")]
        public virtual ActivityConfiguration Configuration { get; set; }

        [XmlElement("InputData", Order = 2)]
        public virtual BindingData InputData { get; set; }

        [XmlElement("InputSchema", Order = 3)]
        public virtual BindingDataSchema InputSchema { get; set; }

        [XmlElement("OutputSchema", Order = 4)]
        public virtual BindingDataSchema OutputSchema { get; set; }

        [XmlArray("Exceptions", Order = 5)]
        [XmlArrayItem("Exception")]
        public List<BindingDataSchema> ExceptionSchemas
        {
            get
            {
                return m_ExceptionSchemas;
            }
            set
            {
                this.m_ExceptionSchemas = value;
            }
        }

        [XmlElement(ElementName = "Render", Type = typeof(ActivityRender), IsNullable = false, Order = 6)]
        public virtual ActivityRender Render
        {
            get
            {
                if (m_Render == null)
                {
                    m_Render = CreateRender();
                }
                return m_Render;
            }
            set
            {
                this.m_Render = value;
            }
        }


        #region protected
        protected virtual ActivityRender CreateRender()
        {
            return new ActivityRender();
        }
        #endregion
    }
}
